<template>
	<view>
		<my-search @click="gotoSearch"></my-search>
		<view class="scroll-container">
			<scroll-view scroll-y class="leftScrollView" :style="{height: wh + 'px;'}">
				<view  v-for="(item, i) in cateList" :key="i" :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChange(i)">{{item.cat_name}}</view>
			</scroll-view>
			<scroll-view scroll-y class="rightScrollView" :style="{height: wh + 'px;'}" :scroll-top="scrollTop">
				<view class="right-scroll-view-item" v-for="(item, i) in cateLevel2" :key="i">
					<text class="title">/ {{item.cat_name}} /</text>
					<view class="cateLevelList3-container">
						<view class="cateLevelList3-item" v-for="(item3, i3) in item.children" :key="i3" @click="gotoGoodsList(item3)">
							<image :src="item3.cat_icon"></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import tabbarBadge from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [tabbarBadge],
		data() {
			return {
				wh: 0,
				cateList: [],
				active: 0,
				cateLevel2: [],
				scrollTop: 0
			};
		},
		onLoad() {
			this.wh = uni.getSystemInfoSync().windowHeight
			this.getCateList()
		},
		methods: {
			async getCateList() {
				const { data: res } = await uni.$http.get('/api/public/v1/categories')
				if(res.meta.status !== 200) return uni.$showMsg()
				this.cateList = res.message
				this.cateLevel2 = this.cateList[0].children
			},
			activeChange(i) {
				this.active = i
				this.cateLevel2 = this.cateList[i].children
				this.scrollTop = this.scrollTop === 0 ? 1 : 0 
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-container {
	display: flex;
	.leftScrollView {
		width: 120px;
		.left-scroll-view-item {
			line-height: 60px;
			background-color: #f7f7f7;
			text-align: center;
			font-size: 12px;
			&.active {
				background-color: #ffffff;
				position: relative;
				&::before {
					position: absolute;
					left: 0;
					top: 10px;
					content: '';
					width: 3px;
					height: 40px;
					background-color: #c00000;
				}
			}
		}
	}
	.rightScrollView {
		.right-scroll-view-item {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			.title {
				margin: 10px 0;
			}
			.cateLevelList3-container {
				display: flex;
				flex-wrap: wrap;
				.cateLevelList3-item {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					margin-bottom: 10px;
					width: 33.33%;
					image {
						width: 100rpx;
						height: 100rpx;
					}
					text {
						font-size: 12px;
					}
				}
			}
		}
	}
}
</style>
